﻿namespace MvcWebRole4.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Security.Cryptography;
    using System.Web.Http;
    using Models;
    using Utility;

    public class MineSweepingRankingController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<MineSweepingRankingItem> Get()
        {
            var ret = new List<MineSweepingRankingItem>();
            var db = new MineSweepingRankingEntities();

            var q = from i in db.MineSweepingRankings
                    group i by i.Level into g
                    select new
                    {
                        Key = g.Key,
                        Items = g.OrderBy(x => x.Score).Take(100)
                    };

            foreach (var g in q)
            {
                foreach (var i in g.Items)
                {
                    ret.Add(new MineSweepingRankingItem
                    {
                        name = i.Name,
                        country = i.Country,
                        level = i.Level,
                        score = i.Score
                    });
                }
            }

            return ret.ToArray();
        }

        // GET api/<controller>/5
        public IEnumerable<MineSweepingRankingItem> Get(int id)
        {
            var db = new MineSweepingRankingEntities();

            var q = from i in db.MineSweepingRankings
                    where i.Level == id
                    orderby i.Score
                    select new MineSweepingRankingItem
                    {
                        name = i.Name,
                        country = i.Country,
                        level = i.Level,
                        score = i.Score
                    };

            return q.Take(100).ToArray();
        }

        // POST api/<controller>
        public void Post(MineSweepingRankingNewItem value)
        {
            if (value == null)
            {
                return;
            }

            var newItem = value.ConvertToEntity();

            if (newItem == null)
            {
                return;
            }

            var db = new MineSweepingRankingEntities();

            var same = from i in db.MineSweepingRankings
                       where i.Name == newItem.Name
                           && i.Country == newItem.Country
                           && i.Level == newItem.Level
                           && i.Score == newItem.Score
                       select i;

            if (same.Any())
            {
                return;
            }

            db.MineSweepingRankings.AddObject(newItem);
            db.SaveChanges();
        }

        // PUT api/<controller>/5
        //public void Put(int id, string value)
        //{
        //}

        // DELETE api/<controller>/5
        //public void Delete(int id)
        //{
        //}
    }
}